import os
import sys
#sys.path.append('..')
sys.path.append('../src')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

import json

from customer.models import CarBrandModel,CarModelModel

with open('car_brand.json') as f:
    brands=json.load(f).get('RECORDS')

no_id_map={}
for brand in brands:
    no_id_map[brand.get('brand_no')]=brand.get('id')
    
    obj,_ = CarBrandModel.objects.get_or_create(id=brand.get('id'))
    obj.name=brand.get('brand_name','')
    obj.jp=brand.get('brand_jp','')
    obj.qp=brand.get('brand_qp','')
    obj.logo= brand.get('brand_logo') if brand.get('brand_logo') else ''
    obj.cls=brand.get('brand_class','')
    obj.kind=brand.get('brand_type','')
    obj.save()

for brand in brands:
    if brand.get('brand_parentno'):
        par_no = brand.get('brand_parentno')
        par_id = no_id_map[par_no]
        par=CarBrandModel.objects.get(id=par_id)
        obj = CarBrandModel.objects.get(id=brand.get('id'))
        obj.parent=par
        obj.save()

with open('car_model.json') as f:
    car_models=json.load(f).get('RECORDS')

md_id_map={}
for md in car_models:
    md_id_map[md.get('model_no')]=md.get('id')
    obj,_ = CarModelModel.objects.get_or_create(id=md.get('id'))
    obj.brand_id=no_id_map[ md.get('brand_no')]
    obj.name=md.get('model_name','')
    obj.jp=md.get('model_jp','')
    obj.qp=md.get('model_qp','')
    obj.img=md.get('model_pic','')
    obj.min_price=md.get('model_minprice','')
    obj.max_price=md.get('model_maxprice','')
    obj.save()
    